package com.tamartech.greendb.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;

import com.tamartech.greendb.data.Address;


public class AddressDAO extends DAO
{
   public Address save (Address a) 
         throws Exception
   {
      try
      {
         begin();
         getSession().save(a);
         commit();
         return a;
      }
      catch (HibernateException e)
      {
         rollback();
         throw new Exception("Could not create address", e);
      }
   }


   public void update(Address a) throws Exception 
   {
      try 
      {
         begin();
         getSession().update(a);
//         getSession().save(a);
         commit();
      } 
      catch (HibernateException e) 
      {
         rollback();
         throw new Exception("Could not update address", e);
      }
   }

   public Address get (Long id) throws Exception 
   {
      try 
      {
         begin();
         Address a = (Address) getSession().load(Address.class, id);
         commit();
         return a;
      } 
      catch (HibernateException e) 
      {
         rollback();
         throw new Exception("Could not obtain address " + id, e);
      }
   }
/*
   public Address find (Address a) throws Exception 
   {
      try 
      {
         begin();
         
         String hql = "from Address where ";
         
         hql += getAddressCondition (1, a.getAddress1());
         hql += " and " + getAddressCondition (2, a.getAddress2());
         hql += " and " + getAddressCondition (3, a.getAddress3());
         hql += " and " + getAddressCondition (4, a.getAddress4());
         hql += " and " + getAddressCondition (5, a.getAddress5());
         hql += " and postcode = :postcode ";
         
         Query q = getSession().createQuery(hql);
         
         q.setString ("address1", a.getAddress1());
         q.setString ("address2", a.getAddress2());
         q.setString ("address3", a.getAddress3());
         q.setString ("address4", a.getAddress4());
         q.setString ("address5", a.getAddress5());
         q.setString ("postcode", a.getPostcode());
         
         Address found = (Address) q.uniqueResult();
         commit();
         return found;
      } 
      catch (HibernateException e) 
      {
         rollback();
         throw new Exception("Could not obtain address " + a, e);
      }
   }
   
   public static String getAddressCondition (int line, String value)
   {
       String condition = "address" + line;
       
       if (value == null)
           condition += " is null";
       else
           condition += " = :address" + line;
       
       return condition;
   }
*/   
   
   public Address find (Address a) throws Exception 
   {
      try 
      {
         begin();
         Query q = getSession().createQuery("from Address where address1 = :address1 " +
                                                          "and address2 = :address2 " +
                                                          "and address3 = :address3 " +
                                                          "and address4 = :address4 " +
                                                          "and address5 = :address5 " +
                                                          "and postcode = :postcode ");
         q.setString ("address1", a.getAddress1());
         q.setString ("address2", a.getAddress2());
         q.setString ("address3", a.getAddress3());
         q.setString ("address4", a.getAddress4());
         q.setString ("address5", a.getAddress5());
         q.setString ("postcode", a.getPostcode());
         Address found = (Address) q.uniqueResult();
         commit();
         return found;
      } 
      catch (HibernateException e) 
      {
         rollback();
         throw new Exception("Could not obtain address " + a, e);
      }
   }
   
/*
   public Address get (String name) throws Exception 
   {
      try 
      {
         begin();
         Query q = getSession().createQuery("from Address where name = :name");
         q.setString ("name", name);
         Address a = (Address) q.uniqueResult();
         commit();
         return a;
      } 
      catch (HibernateException e) 
      {
         rollback();
         throw new Exception("Could not obtain address " + name, e);
      }
   }
*/
   @SuppressWarnings("unchecked")
   public List<Address> list() throws Exception 
   {
      try 
      {
         begin();
         Query q = getSession().createQuery("from Address");
         List<Address> list = q.list();
         commit();
         return list;
      } 
      catch (HibernateException e) 
      {
         rollback();
         throw new Exception("Could not list the addresss", e);
      }
   }

   
   public void delete(Address a)
      throws Exception
   {
      try
      {
         begin();
         getSession().delete(a);
         commit();
      }
      catch (HibernateException e)
      {
         rollback();
         throw new Exception("Could not delete address", e);
      }
   }
}
